<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="业务申报" prop="ywSb">
            <el-input
              v-model="queryParams.ywSb"
              placeholder="请输入业务申报"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业务类型" prop="ywLx">
            <el-input
              v-model="queryParams.ywLx"
              placeholder="请输入业务类型"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划类别" prop="jhLb">
            <el-input
              v-model="queryParams.jhLb"
              placeholder="请输入计划类别"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-form :model="queryParams" size="mini" label-width="170px">
      <el-row :gutter="10" class="mb8" style="display: flex; justify-content: flex-end;margin-bottom: -8px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['xtgl:ywjh:add']"
          >新增年度业务计划
          </el-button>
        </el-col>
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="primary"-->
<!--            icon="el-icon-download"-->
<!--            size="mini"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['xtgl:ywjh:export']"-->
<!--          >导出-->
<!--          </el-button>-->
<!--        </el-col>-->
        <el-col :span="1.5" style="margin-left: auto;">
          <el-form-item label="计划年度:">
            <vxe-select @change="getList" size="mini" v-model="queryParams.nd" placeholder="请选择计划年度">
              <vxe-option v-for="dict in dict.type.year" :key="dict.value" :value="dict.value"
                          :label="dict.label"></vxe-option>
            </vxe-select>
          </el-form-item>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <!--    <el-table :span-method="objectSpanMethod"-->
    <!--              height="300" class="fixedTableOne" v-loading="loading" :data="ywjhList"-->
    <!--              @selection-change="handleSelectionChange">-->
    <!--&lt;!&ndash;      <el-table-column type="selection" width="55" align="center"/>&ndash;&gt;-->
    <!--      <el-table-column label="业务申报" align="center" prop="ywSb"/>-->
    <!--      <el-table-column label="年度" align="center" prop="nd"/>-->
    <!--      <el-table-column label="业务类型" align="center" prop="ywLx"/>-->
    <!--      <el-table-column label="计划类别" align="center" prop="jhLb"/>-->
    <!--      <el-table-column label="开始日期" align="center" prop="ksRq" width="180">-->
    <!--        <template slot-scope="scope">-->
    <!--          <span>{{ parseTime(scope.row.ksRq, '{y}-{m}-{d}') }}</span>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="结束日期" align="center" prop="jsRq" width="180">-->
    <!--        <template slot-scope="scope">-->
    <!--          <span>{{ parseTime(scope.row.jsRq, '{y}-{m}-{d}') }}</span>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="排序" align="center" prop="px"/>-->
    <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-button-->
    <!--            size="mini"-->
    <!--            type="text"-->
    <!--            icon="el-icon-edit"-->
    <!--            @click="handleUpdate(scope.row)"-->
    <!--            v-hasPermi="['xtgl:ywjh:edit']"-->
    <!--          >修改-->
    <!--          </el-button>-->
    <!--          <el-button-->
    <!--            size="mini"-->
    <!--            type="text"-->
    <!--            icon="el-icon-delete"-->
    <!--            @click="handleDelete(scope.row)"-->
    <!--            v-hasPermi="['xtgl:ywjh:remove']"-->
    <!--          >删除-->
    <!--          </el-button>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--    </el-table>-->

    <MergeTable :empty-text="'该年度暂无年度计划, 请新增'" highlight-current-row
                :merge-arr="ckMergeArr"
                v-loading="loading" :table-data="ywjhList">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <!--      <el-table-column label="序号" align="center" type="index"/>-->
      <!--      <el-table-column show-overflow-tooltip label="仓库ID" align="center" prop="ckId"/>-->
      <el-table-column label="业务申报" align="center" prop="ywSb"/>
      <el-table-column label="年度" align="center" prop="nd" width="100"/>
      <el-table-column label="业务类型" align="center" prop="ywLx"/>
      <el-table-column label="计划类别" align="center" prop="jhLb"/>
      <el-table-column label="开始日期" align="center" prop="ksRq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ksRq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="jsRq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jsRq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="zt">
        <template slot-scope="scope">
          <el-switch
            @change="updateZt(scope.row)"
            v-model="scope.row.zt"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="启用"
            inactive-value="停用">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="计划状态" align="center" prop="jhZt">
        <template slot-scope="scope">
          <span style="color: #13ce66" v-if="scope.row.jhZt == '进行中'">{{ scope.row.jhZt }}</span>
          <span style="color: #ff0000" v-else>{{ scope.row.jhZt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleSbSxUpdate(scope.row)"
          >更改申报时限
          </el-button>
        </template>
      </el-table-column>
    </MergeTable>

    <!-- 分页组件 -->
    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->

    <!-- 添加或修改业务计划对话框 -->
    <vxe-modal :title="title" v-model="open" width="500px" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年度" prop="nd">
          <el-select v-model="form.nd" placeholder="请选择年度">
            <el-option
              v-for="dict in dict.type.year"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>


    <!-- 更改申报时限对话框 -->
    <vxe-modal :title="title" v-model="sbsx" width="500px" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="开始日期" prop="ksRq">
              <el-date-picker clearable style="width: 100%;"
                              v-model="form.ksRq"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="jsRq">
              <el-date-picker clearable style="width: 100%;"
                              v-model="form.jsRq"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

  </div>
</template>

<script>
import {delYwjh, getYwjh, insertNdYwjh, listYwjh, updateYwjh} from "@/api/xtgl/ywjh";
import MergeTable from "@/components/MergeTable/index.vue";

export default {
  name: "Ywjh",
  components: {MergeTable},
  dicts: ['year', 'zt'],
  data() {
    return {
      zt: '启用',
      sbsx: false,
      ckMergeArr: ['ywSb'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务计划表格数据
      ywjhList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        ywSb: null,
        nd: new Date().getFullYear().toString(),
        ywLx: null,
        jhLb: null,
        ksRq: null,
        jsRq: null,
        px: null,
        idWhere: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ksRq: [
          {required: true, message: "开始日期不能为空", trigger: "blur"}
        ],
        jsRq: [
          {required: true, message: "结束日期不能为空", trigger: "blur"}
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    updateZt(row) {
      if (row.zt == '启用') {
        let form = {
          ywjhId: row.ywjhId,
          zt: '启用'
        }
        updateYwjh(form).then(response => {
          this.$modal.msgSuccess("启用成功");
          this.getList();
        });
      }else if (row.zt == '停用'){
        let form = {
          ywjhId: row.ywjhId,
          zt: '停用'
        }
        updateYwjh(form).then(response => {
          this.$modal.msg("停用成功");
          this.getList();
        });
      }
      console.log(row)
    },
    /** 查询业务计划列表 */
    getList() {
      this.loading = true;
      listYwjh(this.queryParams).then(response => {
        this.ywjhList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.sbsx = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ywjhId: null,
        ywSb: null,
        nd: this.queryParams.nd,
        ywLx: null,
        jhLb: null,
        ksRq: null,
        jsRq: null,
        px: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,
        zt: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ywjhId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增年度业务计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ywjhId = row.ywjhId || this.ids
      getYwjh(ywjhId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改业务计划";
      });
    },
    /** 更改申报时限 */
    handleSbSxUpdate(row) {
      this.reset();
      const ywjhId = row.ywjhId || this.ids
      getYwjh(ywjhId).then(response => {
        this.form = response.data;
        this.sbsx = true;
        this.title = "更改申报时限";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.idWhere != null) {
            updateYwjh(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.sbsx = false;
              this.getList();
            });
          } else {
            insertNdYwjh(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ywjhIds = row.ywjhId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delYwjh(ywjhIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xtgl/ywjh/export', {
        ...this.queryParams
      }, `ywjh_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
